package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.CgnciBusinessInsuranceSaveOrUpdateDTO;
import com.zatech.cgnci.project.dto.SolvencyAdequacySaveOrUpDTO;
import com.zatech.cgnci.project.entity.CgnciAccessoryDO;
import com.zatech.cgnci.project.entity.CgnciSolvencyAdequacyDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.mapper.CgnciSolvencyAdequacyMapper;
import com.zatech.cgnci.project.service.CgnciAccessoryService;
import com.zatech.cgnci.project.service.CgnciSolvencyAdequacyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.vo.CgnciAccessoryVO;
import com.zatech.cgnci.project.vo.SolvencyAdequacyVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 偿付能力充足率 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2025-02-26
 */
@Service
public class CgnciSolvencyAdequacyServiceImpl extends ServiceImpl<CgnciSolvencyAdequacyMapper, CgnciSolvencyAdequacyDO> implements CgnciSolvencyAdequacyService {

    @Resource
    CgnciAccessoryService accessoryService;


    @Override
    public List<SolvencyAdequacyVO> listAdequacy(String annual) {
        List<CgnciSolvencyAdequacyDO> solvencyAdequacyDOS = this.baseMapper.selectList(new LambdaQueryWrapper<CgnciSolvencyAdequacyDO>()
                .eq(VerifyUtils.isNotEmpty(annual), CgnciSolvencyAdequacyDO::getAnnual, annual));
        if (VerifyUtils.isEmpty(solvencyAdequacyDOS)) {
            return null;
        }
        List<SolvencyAdequacyVO> solvencyAdequacyVOS = ConvertUtils.convert(solvencyAdequacyDOS, SolvencyAdequacyVO.class);
        solvencyAdequacyVOS.forEach(solvencyAdequacyVO -> {
            List<CgnciAccessoryDO> accessoryDOList = accessoryService.list(new LambdaQueryWrapper<CgnciAccessoryDO>().eq(CgnciAccessoryDO::getBusinessId, solvencyAdequacyVO.getId()));
            if (VerifyUtils.isNotEmpty(accessoryDOList)) {
                List<CgnciAccessoryVO> accessoryVOList = ConvertUtils.convert(accessoryDOList, CgnciAccessoryVO.class);
                solvencyAdequacyVO.setAccessoryVOList(accessoryVOList);
            }
        });
        return solvencyAdequacyVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveOrUpdateSolvencyAdequacy(SolvencyAdequacySaveOrUpDTO saveOrUpDTO) {
        UserEntity user = UserContext.getUser();
        CgnciSolvencyAdequacyDO adequacyDO = ConvertUtils.convert(saveOrUpDTO, CgnciSolvencyAdequacyDO.class);
        if (saveOrUpDTO.getId() != null) {
            adequacyDO.setUpdateUserName(user.getNameAndCode());
        } else {
            adequacyDO.setCreateUserName(user.getNameAndCode());
        }
        this.saveOrUpdate(adequacyDO);

        // 文件信息
        List<SolvencyAdequacySaveOrUpDTO.AccessoryDTO> accessoryDTOList = saveOrUpDTO.getAccessoryDTOList();
        if (VerifyUtils.isNotEmpty(accessoryDTOList)) {
            List<CgnciAccessoryDO> accessoryList = new ArrayList<>();
            accessoryDTOList.forEach(accessory -> {
                CgnciAccessoryDO accessoryDO = accessoryService.getById(accessory.getAccId());
                // 文件绑定业务数据
                accessoryDO.setBusinessId(adequacyDO.getId());
                accessoryList.add(accessoryDO);
            });
            accessoryService.saveOrUpdateBatch(accessoryList);
        }
        return adequacyDO.getId();
    }
}